package com.niuniu;

import com.xingli.zhajinhua.beans.Card;
import com.xingli.zhajinhua.beans.PlayerCards;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardColor;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardPoint;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardType;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardColor;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardPoints;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardType;
import com.zhu.spider.ExcelExport;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZjhExport {

    private static int[] winPlayRecord = new int[]{0,0,0};
    private static Map<Integer,Integer> continueWinMap = new HashMap<>();

//    总下注：500000000  2.9
//    赢：482274976
//    利润：17725024
//    特殊牌型：500000
//    赢的次数：333342

//    总下注：500000000
//    赢：475017536
//    利润：24982464
//    特殊牌型：460089
//    赢的次数：332939

    public static void main(String[] args) throws IOException {
        int length = 500000;
        int betCount=500;
        ZhaJinHuaLogic zjh = new ZhaJinHuaLogic();
        int winMoney= 0 ;
        int specialCount=0;
        int winCount = 0;
        for (int i = 0; i < length; i++) {
            List<Card> cardList = zjh.getNewCardCollectionAndShuffle();
            List<Card[]> cardsList = zjh.sendCards(cardList);
            List<PlayerCards> niuCardsDataList = zjh.compareCards(cardsList);

            int notBetIndex = getNotBetIndex();
            float odds = getZjhOdds();
            if (odds != 3.0f){
                specialCount++;
            }
            if (!niuCardsDataList.get(notBetIndex).isResult())  {
                //赢了
                winCount++;
                winMoney+=odds*betCount;
            }

        }
        System.out.println("总下注："+length*500*2);
        System.out.println("赢："+winMoney);
        System.out.println("利润："+(length*500*2-winMoney));
        System.out.println("特殊牌型："+specialCount);
        System.out.println("赢的次数："+winCount);

    }

    private static int getNotBetIndex(){
        int random = (int) (Math.random()*3);
        if (random==3){
            System.out.println("可以为3？？？");
        }
        return random;
    }

    private static float getZjhOdds(){
        String oddsString = "58,34,8";
        String[] oddsArr = oddsString.split(",");
        int random = (int) (Math.random()*100);
//        return 2.9f;
        if (random<Integer.parseInt(oddsArr[0])){
            return 2.8f;
        }else if (random<Integer.parseInt(oddsArr[0])+Integer.parseInt(oddsArr[1])){
            return 2.9f;
        }else {
            return 3.0f;
        }
    }
    private static void exportToExcel(){
        int length = 50000;
        ExcelExport excelExport = new ExcelExport("E:\\炸金花-"+length+"条.xlsx");
        String[] head = new String[]{"玩家1牌","类型","玩家2牌","类型","玩家3牌","类型","赢家"};
        excelExport.setRowContent(head);
        for (int i = 0; i < length; i++) {
            ZhaJinHuaLogic zjh = new ZhaJinHuaLogic();
            List<Card> cardList = zjh.getNewCardCollectionAndShuffle();
            List<Card[]> cardsList = zjh.sendCards(cardList);
            List<PlayerCards> niuCardsDataList = zjh.compareCards(cardsList);
            String[] str = new String[7];
            str[0] = getCardStr(niuCardsDataList.get(0).getCards());
            str[1] = getCardTypeStr(niuCardsDataList.get(0).getCardsType());

            str[2] = getCardStr(niuCardsDataList.get(1).getCards());
            str[3] = getCardTypeStr(niuCardsDataList.get(1).getCardsType());

            str[4] = getCardStr(niuCardsDataList.get(2).getCards());
            str[5] = getCardTypeStr(niuCardsDataList.get(2).getCardsType());
            String win = "";
            int winIndex = 0;
            if (niuCardsDataList.get(0).isResult())  {win = "玩家1";winIndex=0;}
            if (niuCardsDataList.get(1).isResult())  {win = "玩家2";winIndex=1;}
            if (niuCardsDataList.get(2).isResult())  {win = "玩家3";winIndex=2;}
            str[6] = win;

            calContinueWinMap(winIndex);

            excelExport.setRowContent(str);
            System.out.println(i);
        }
        excelExport.setRowContent(continueWinMap.toString());

        excelExport.exportContent();
    }

    private static void calContinueWinMap(int winIndex){
        // winIndex 1
        int temp = winPlayRecord[winIndex];
        for (int i = 0; i < 3; i++) {
            if (winIndex != i){
                continueWinMap.putIfAbsent(winPlayRecord[i],0);
                continueWinMap.put(winPlayRecord[i],continueWinMap.get(winPlayRecord[i])+1);
            }
            winPlayRecord[i] = 0;
        }
        winPlayRecord[winIndex] = temp+1;
    }

    private static String getCardStr(Card[] cards){
        StringBuilder str = new StringBuilder();
        for (Card card:cards){
            str.append(ZhaCardColor.values()[card.getColor()].name+ ZhaCardPoints.values()[card.getPoint()].name+" ");
        }
        return str.toString();
    }
    private static String getCardTypeStr(String cardType){
        return ZhaCardType.valueOf(cardType).name;
    }
}
